--
-- Author: shunguo.chen
-- Date: 2016-06-12 14:22:24
--

local LocalNotificationAPI = class("LocalNotificationAPI")

local javaclassName = "com/repong/notify/LocalNotificationAPI"
local ocClassName = "LocalNotificationAPI"

function LocalNotificationAPI:ctor()
	self.conf = app:loadResScript("scripts.config.notifyconf") or {}
end

function LocalNotificationAPI:registShedules()
	self:cancelAll()
	
	if nil == self.conf.shedules then 
		print("LocalNotificationAPI conf has no shedules")
		return 
	end

	for i,v in ipairs(self.conf.shedules) do
		self:show(v)
	end
end

--params:
--delay second
function LocalNotificationAPI:registEvent(name, delay)
	if nil == self.conf.events or nil == self.conf.events[name] then 
		print("LocalNotificationAPI conf has no events or name="..name)
		return 
	end
	local data = self.conf.events[name]
	local params = {tag=data.tag, message=data.message, delay=delay}
	self:show(params)
end

function LocalNotificationAPI:show(params)
	if device.platform=="android" then
		local args = {json.encode(params)}
		local sigs = "(Ljava/lang/String;)V"
		local ok = luaj.callStaticMethod(javaclassName,"showLocalNotification",args,sigs)
		if not ok then
			print("LocalNotificationAPI showLocalNotification fail")
		end
	elseif device.platform=="ios" then
		local ok,_ = luaoc.callStaticMethod(ocClassName, "regist", params)
		if not ok then
			print("LocalNotificationAPI regist fail:",ret)
		end
	end
end

function LocalNotificationAPI:cancel(tag)
	if device.platform=="android" then
		local args = {tag}
		local sigs = "(I)V"
		local ok = luaj.callStaticMethod(javaclassName,"cancelLocalNotification",args,sigs)
		if not ok then
			print("LocalNotificationAPI cancelLocalNotification fail")
		end
	elseif device.platform=="ios" then
		local ok,ret = luaoc.callStaticMethod(ocClassName, "cancel", {tag=tag})
		if not ok then
			print("LocalNotificationAPI cancel fail:",ret)
		end
	end
end

function LocalNotificationAPI:cancelAll()
	if device.platform=="android" then
		local args = {}
		local sigs = "()V"
		local ok = luaj.callStaticMethod(javaclassName,"cancelAllLocalNotification",args,sigs)
		if not ok then
			print("LocalNotificationAPI cancelAllLocalNotification fail")
		end
	elseif device.platform=="ios" then
		local ok,ret = luaoc.callStaticMethod(ocClassName, "cancelAll", nil)
		if not ok then
			print("LocalNotificationAPI cancelAll fail:",ret)
		end
	end
end

return LocalNotificationAPI